{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div class=\"contentcontainer med left\" style=\"margin-left: -50px;\">\n",
    "    <dl class=\"dl-horizontal\">\n",
    "      <dt>Title</dt> <dd> RangeXY stream example</dd>\n",
    "      <dt>Description</dt> <dd>A linked streams example demonstrating how to RangeXY to compute histogram of current zoom area.</dd>\n",
    "      <dt>Backends</dt> <dd> Bokeh</dd>\n",
    "      <dt>Tags</dt> <dd> streams, range, interactive</dd>\n",
    "    </dl>\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import holoviews as hv\n",
    "\n",
    "hv.extension('bokeh')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define an image\n",
    "Y, X = (np.mgrid[0:100, 0:100]-50.)/20.\n",
    "img = hv.Image(np.sin(X**2+Y**2))\n",
    "\n",
    "def selected_hist(x_range, y_range):\n",
    "    # Apply current ranges\n",
    "    obj = img.select(x=x_range, y=y_range) if x_range and y_range else img\n",
    "\n",
    "    # Compute histogram\n",
    "    return hv.operation.histogram(obj)\n",
    "\n",
    "# Define a RangeXY stream linked to the image \n",
    "rangexy = hv.streams.RangeXY(source=img)\n",
    "\n",
    "# Adjoin the dynamic histogram computed based on the current ranges\n",
    "img << hv.DynamicMap(selected_hist, streams=[rangexy])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<center><img src=\"https://assets.holoviews.org/gifs/examples/streams/bokeh/range_histogram.gif\" width=400></center>"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
